<template>
  <view class="scroll-list">
	<!-- #ifdef MP-WEIXIN -->
	<transition-group name="scroll" tag="view" class="list">
	  <view v-for="(item, index) in visibleItems" :key="item.id" class="item">
		  <view class="num">+{{getNum(index)}}</view>
		  <view class="text">{{ item.title }}</view>
	  </view>
	</transition-group>
	<!-- #endif -->
	<!-- #ifndef MP-WEIXIN -->
	<view name="scroll" tag="view" class="list">
	  <view v-for="(item, index) in visibleItems" :key="item.id" class="item">
		  <view class="num">+{{getNum(index)}}</view>
		  <view class="text">{{ item.title }}</view>
	  </view>
	</view>
	<!-- #endif -->
  </view>
</template>

<script>
export default {
  name: "scrollList",
  props: {
    items: {
      type: Array,
      required: true,
	  default: ()=>[]
    },
  },
  data() {
    return {
      displayedItems: [], // 当前展示中的队列
      nextIndex: 0, // 下一条要显示的下标
      timer: null,
    };
  },
  computed: {
    visibleItems() {
      return this.displayedItems.slice(-3); // 最多显示3条
    },
  },
  watch: {
    items: {
      immediate: true,
      handler(newVal) {
        this.startScrolling();
      },
    },
  },
  methods: {
	  getNum(index){
		  let num=this.nextIndex>3 ? this.nextIndex-2+index : index+1;
		  return String(num).padStart(2, '0')
	  },
    startScrolling() {
      if (this.timer) clearInterval(this.timer);

      this.timer = setInterval(() => {
        if (this.nextIndex < this.items.length) {
          this.displayedItems.push(this.items[this.nextIndex]);
          this.nextIndex++;
        } else {
          clearInterval(this.timer);
        }
      }, 500);
    },
  },
  beforeUnmount() {
    if (this.timer) clearInterval(this.timer);
  },
};
</script>

<style scoped lang="scss">
.scroll-list {
  height: 330rpx; /* 3行，每行30px */
  overflow: hidden;
}

.list {
	display: flex;
	flex-direction: column;
	gap: 20rpx;
	margin: 0;
	padding: 0;
}

.item{
	display: flex;
	align-items: center;
	height: 90rpx;
	padding: 0 42rpx 0 22rpx;
	background-color: #fff;
	border-radius: 18rpx;
	font-size: 28rpx;
	color: #363636;
}
.text{
	flex: 1;
	margin-left: 14rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.num{
	flex-shrink: 0;
}

/* 动画效果 */
.scroll-enter-active,
.scroll-leave-active {
  transition: all 0.5s ease;
}

.scroll-enter-from {
  transform: translateY(110rpx);
  opacity: 0;
}

.scroll-leave-to {
  transform: translateY(-110rpx);
  opacity: 0;
}
</style>
